#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>

void *Producer(void *arg)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    while (true)
    {
        int x=rand()%10+1;
        int y = rand()%10+1;
        char oper = opers[rand()%(opers.size())];
        Task t(oper, x, y);
        t.GainTask(x,y);//相当于生产者线程在生产数据时要先获取数据!
        bq->Push(t);
    }
    return nullptr;
}
void *Consumer(void *arg)
{
    BlockQueue<Task>* bq= static_cast<BlockQueue<Task>*>(arg);
    while(true)
    {
        Task tmp=bq->Pop();
        tmp();//相当于是消费者线程处理它消费的任务数据
    }
}
int main()
{
    srand(time(nullptr));
    BlockQueue<Task> *bq = new BlockQueue<Task>(5);//主进程在堆区开辟共享资源
    pthread_t p, c;
    pthread_create(&p, nullptr, Producer, bq);
    pthread_create(&c, nullptr, Consumer, bq);

    pthread_join(c, nullptr);//阻塞等待线程
    pthread_join(p, nullptr);
    delete bq;
    return 0;
}
